<!DOCTYPE html>
<html>
	<head th:include="/common/head :: head">
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit|ie-comp|ie-stand">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<meta http-equiv="Cache-Control" content="no-siteapp" />
	</head>
<body>
	<div class="x-body">
		<form class="layui-form" lay-filter="update_form">
			<div class="layui-form-item">
				<label for="id" class="layui-form-label">计划编码
				</label>
				<div class="layui-input-inline">
					<input type="text" id="id" name="id" required=""
						lay-verify=id autocomplete="off" class="layui-input" readOnly="readOnly">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="userName" class="layui-form-label"> <span
					class="x-red">*</span>计划人姓名
				</label>
				<div class="layui-input-inline">
					<select  id="userName" name="userName" lay-search=""></select>
				</div>
			</div>
			<div class="layui-form-item">
				<label for="month" class="layui-form-label"> <span
					class="x-red">*</span>计划月份
				</label>
				<div class="layui-input-inline">
					<input type="text" id="month" name="month" required=""
						lay-verify="month" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="status" class="layui-form-label">计划状态
				</label>
				<div class="layui-input-inline">
					<input type="text" id="status" name="status" required=""
						lay-verify="status" autocomplete="off" class="layui-input"
						readOnly="readOnly">
				</div>
			</div>
			<div class="layui-form-item layui-form-text">
              	<label for="content" class="layui-form-label"><span
					class="x-red">*</span>计划内容
				</label>
              <div class="layui-input-block">
              	   <textarea  id="content" lay-verify="content" name="content" autocomplete="off" class="layui-textarea"></textarea>
              </div>
          </div>
			<div class="layui-form-item">
				<label for="L_repass" class="layui-form-label"> </label>
				<button class="layui-btn" lay-filter="update_form" lay-submit="">保存</button>
				<button class="layui-btn" onclick="black()">返回</button>
			</div>
		</form>
	</div>
</body>
</html>
<script>
	$(function(){
		//初始化
		init();
		
	})
	var planId = sessionStorage.getItem("planId");
	var _this = {};
	var updateFormData={};
	//通过id拿到对象
	function init(){
		$.post("/plan/getPlanById","planId="+planId,function(result){
			if(result.status==0){
				//初始化修改表单（赋值）
				_this.plan=result.data;
				//获取用户姓名、id下拉表
				getUserId();
				//初始化修改表单（赋值）
				initUpdateForm(result.data);
			}else{
				layer.msg(result.msg);
			}
		})
	}
	//初始化修改表单（赋值）
	function initUpdateForm(plan){
		layui.use(['form','layer'],function(){
			$=layui.jquery;
			var form = layui.form,
			layer = layui.layer;
			//自定义验证
			form.verify({
				userName:function(value){
					if(value.length<2){
						return "昵称至少得2个字符";
					}
				},
				month:function(value){
					if(value==null||value==""){
						return "月份不能为空";
					}
				},
				content:function(value){
					if(value==null||value==""){
						return "内容不能为空";
					}
				}
			})
			//赋值
			form.val('update_form',{
				"id":plan.id,
				"month":plan.month,
				"content":plan.content
			})
			if(plan.status==1){
				form.val("update_form",{
					"status":"未审核"
				})
			}
			if(plan.status==2){
				form.val("update_form",{
					"status":"已通过"
				})
			}else{
				form.val("update_form",{
					"status":"未通过"
				})
			}
			//监听提交
			form.on("submit(update_form)",function(data){
				console.log(data.field);
				updateFormData = data.field;
				updatePlan();
				return false;
			})
		})
	}
	//提交修改操作
	function updatePlan(){
		$.ajax({
			url:"/plan/updatePlan",
			type:"post",
			data:{
				"id":planId,
				"userName":updateFormData.userName,
				"month":updateFormData.month,
				"content":updateFormData.content
			},
			success:function(result){
				if(result.status==0){
					layer.msg(result.msg,function(){
						//关闭弹出层
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);
					})
				}else{
					layer.msg(result.msg);
				}
			}
		})
	}
	//返回
	function black(){
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);
	}
	//年月选择器
	layui.use('laydate', function(){
	    var laydate = layui.laydate;
		laydate.render({
		    elem: '#month'
		    ,type: 'month'
		  });
	});
	//异步改为同步  async:false;
	//获取用户姓名、id列表
	function getUserId(){
		$.ajax({
			url:"/user/getUserIds",
			type:"post",
			async:false,
			success:function(result){
				if(result.status==0){
					showUserName(result);	
				}else{
					layer.msg(result.msg);
				}
			}
		})
	}
	//展示下拉框
	function showUserName(result){
		var str= "";
		var list = result.data;
		for(i in list){
			if(_this.plan.userName==list[i].userName){
				str+='<option selected="selected" value="'+list[i].id+'">'+_this.plan.userName+'</option>';
			}else{
				str+="<option  value="+list[i].id+">"+list[i].userName+"</option>";
			}
		}
		$("#userName").append(str);
	}
</script>
